@echo off
title Hello Frontend Tool

rem =================================================================
rem FEATURES:
rem 1. One-click to start development server
rem 2. Multiple browser opening methods (for maximum compatibility)
rem 3. Compatible with all Node.js versions
rem 4. Multiple fallbacks if one method fails
rem =================================================================

echo ====================================
echo    Hello Frontend Startup Tool
echo ====================================
echo.

rem Change to script directory
cd /d %~dp0

rem Check dependencies
if not exist node_modules (
  echo [INFO] Dependencies not installed. Installing now...
  call npm install --registry=https://registry.npmmirror.com
  if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] Failed to install dependencies.
    pause
    exit /b 1
  )
  echo [SUCCESS] Dependencies installed.
  echo.
)

rem Create a temporary HTML file that will auto-redirect to our app
echo ^<!DOCTYPE html^> > redirect.html
echo ^<html^> >> redirect.html
echo ^<head^> >> redirect.html
echo ^<meta http-equiv="refresh" content="0;URL='http://localhost:80/'" /^> >> redirect.html
echo ^<title^>Redirecting...^</title^> >> redirect.html
echo ^</head^> >> redirect.html
echo ^<body^> >> redirect.html
echo ^<p^>If you are not redirected automatically, click ^<a href="http://localhost:80/"^>here^</a^>.^</p^> >> redirect.html
echo ^<p^>If that doesn't work, try ^<a href="http://localhost:81/"^>this link instead^</a^>.^</p^> >> redirect.html
echo ^</body^> >> redirect.html
echo ^</html^> >> redirect.html

echo [INFO] Starting development server...
echo [INFO] A browser window will open in a moment...
echo [INFO] Press Ctrl+C to stop the server when done
echo.

rem Method 1: Open the redirect page directly in a new window
start redirect.html

rem Method 2: Start another CMD window to open the browser after a delay
@REM start cmd /c "echo Waiting 15 seconds before opening browser... && ping 127.0.0.1 -n 16 > nul && explorer http://localhost:80/ && exit"

echo [INFO] Browser windows should open automatically
echo [INFO] If browser doesn't open or shows blank page, manually visit:
echo [INFO] http://localhost:80/ or http://localhost:81/
echo.

rem 修复: 直接设置环境变量并运行vue-cli-service (Windows兼容方式)
set NODE_OPTIONS=--openssl-legacy-provider
call node_modules\.bin\vue-cli-service serve

rem Check if the process ended with an error
if %ERRORLEVEL% NEQ 0 (
  echo.
  echo [ERROR] Server stopped unexpectedly with error code %ERRORLEVEL%
  echo [INFO] Please check the error messages above.
)

rem Clean up
if exist redirect.html del redirect.html

echo.
echo [INFO] Server has been stopped.
echo Press any key to exit...
pause > nul